<template>
  <div class="trips-list">
    <basic-list @load="loadMore" :isFinished="isFinished" :isLoading="isLoading" :isEmpty="!isLoading&&trips.length == 0">
      <trip-item v-for="item in trips" :key="item.productCode" :label="params.name" :item="item" @click="handleClick(item)"/>
    </basic-list>
  </div>
</template>
<script>

import sdk from 'sdk'
import tripItem from '@/components/business/product-item/trip-item'
import sleep from '@/utils/helper/sleep'
import {encodeQuery} from 'utils/helper/query'
import { searchRoutes } from '@/api/index'

export default {
  name: 'trips-list',
  components: {
    tripItem
  },

  props: {
    params: { type: Object, default: () => {} }
  },

  data() {
    return {
      trips: [],
      isFinished: false,
      isLoading: false,
      routeParams: {}
    }
  },
  watch:{
    params: {
      handler: function(val){
        if(!val.regionId) return
        this.isFinished = false
        this.routeParams = { ...val }
        this.trips = []
        this.loadMore()
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    loadRoutes() {
      if(!this.routeParams.pageSize ||this.isFinished) return
      this.isLoading = true  
      searchRoutes(this.routeParams).then(res=>{
        this.isLoading = false

        if(this.routeParams.pageNum <= 1) {
          this.trips = res.result.routeInfos
        } else {
          this.trips = this.trips.concat(res.result.routeInfos)
        }

        if(!res.result.routeInfos || !res.result.routeInfos.length || res.result.routeInfos.length < this.routeParams.pageSize) {
          this.isFinished = true
        }

        this.routeParams.pageNum = this.routeParams.pageNum + 1
      })
    },
    loadMore() {
      if (this.isLoading || this.isFinished) return 
      this.loadRoutes()
    },

    /**
     * 点击列表项
     * @param {Object} item 列表项对象 
     */
    handleClick(item) {
      let path = ''
      let queryStr = encodeQuery({
        code: item.productCode,
        authorization:''
      })
      sdk.navigateTo({
        url: `/packageDetail/pages/tourline/detail/index?${queryStr}`
      })
    }
  }
}
</script>
<style lang="less" scoped>

</style>